class RenameImagesToMedias < ActiveRecord::Migration
  def self.up
    execute "alter table image_files drop foreign key image_files_ibfk_1"
    execute "alter table comments drop foreign key comments_ibfk_2"
    execute "alter table image_rating_logs drop foreign key image_rating_logs_ibfk_2"
    add_column :images, :mediable_type, :string
    add_column :images, :mediable_id, :integer, :references => nil
    rename_column :image_files, :image_id, :media_id
    rename_column :comments, :image_id, :media_id
    rename_column :image_rating_logs, :image_id, :media_id
    rename_table :image_rating_logs, :media_rating_logs
    rename_table :images, :medias
    ImageFile.find(:all, :conditions => 'parent_id IS NULL').each do |image_file|
      puts image_file.media_id
      media = Media.find(image_file.media_id)
      puts media.inspect
      media.update_attributes(:mediable_type => 'ImageFile', :mediable_id => image_file.id)
    end
    Tagging.find(:all).each do |tagging|
      tagging.update_attributes(:taggable_type => 'Media')
    end
    FulltextIndex.find(:all).each do |fulltext|
      fulltext.update_attributes(:indexable_type => 'Media')
    end
    Viewing.find(:all).each do |viewing|
      viewing.update_attributes(:viewable_type => 'Media')
    end
    remove_column :image_files, :media_id
    execute "alter table comments add constraint comments_ibfk_2 foreign key (media_id) references medias(id)"
    execute "alter table media_rating_logs add constraint image_rating_logs_ibfk_2 foreign key (media_id) references medias(id)"
  end

  def self.down
    add_column :image_files, :media_id, :integer
    execute "alter table comments drop foreign key comments_ibfk_2"
    execute "alter table media_rating_logs drop foreign key image_rating_logs_ibfk_2"
    execute "alter table image_files drop foreign key image_files_ibfk_3"
    Tagging.find(:all).each do |tagging|
      tagging.update_attributes(:taggable_type => 'Image')
    end
    FulltextIndex.find(:all).each do |fulltext|
      fulltext.update_attributes(:indexable_type => 'Image')
    end
    Viewing.find(:all).each do |viewing|
      viewing.update_attributes(:viewable_type => 'Image')
    end
    Media.find(:all).each do |media|
      image = ImageFile.find(media.mediable_id)
      image.update_attributes(:media_id => media.id)
    end
    rename_column :comments, :media_id, :image_id
    rename_column :image_files, :media_id, :image_id
    rename_column :media_rating_logs, :media_id, :image_id
    remove_column :medias, :mediable_type
    remove_column :medias, :mediable_id
    rename_table :media_rating_logs, :image_rating_logs
    rename_table :medias, :images
    execute "alter table image_files add constraint image_files_ibfk_1 foreign key (image_id) references images(id)"
    execute "alter table comments add constraint comments_ibfk_2 foreign key (image_id) references images(id)"
    execute "alter table image_rating_logs add constraint image_rating_logs_ibfk_2 foreign key (image_id) references images(id)"
  end
end
